package calao.client.ui;

import java.util.Date;

import calao.client.util.CalendarDate;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Widget;

public class Format 
{
	
	static public final Format STRING = new Format() {
		public String formatAsText(Object object) {
			return object.toString();
		}
	};

	static public final NumberFormat NUMBER = new NumberFormat();

	final static DateTimeFormat dateFormat = DateTimeFormat.getShortDateFormat();
	final static DateTimeFormat timeFormat = DateTimeFormat.getShortTimeFormat();

	static public final Format DATE_AND_TIME = new Format() {
		public String formatAsText(Object object) {
			Date date = toDate(object);
			return dateFormat.format(date) + " " + timeFormat.format(date);
		}
	};
		
	static public final Format DATE = new Format() {
		public String formatAsText(Object object) {
			Date date = toDate(object);
			return dateFormat.format(date);
		}
	};
	
	static public final Format WIDGET = new Format() {
		public Widget formatAsWidget(Object obj) {
			return (Widget)obj;
		}		
	};

	protected Format() { }

	public String formatAsText(Object object)
	{
		return null;
	}
	
	public Widget formatAsWidget(Object object)
	{
		return null;
	}

	public String formatAsText(int value)
	{
		return formatAsText(new Integer(value));
	}
	
	public Widget formatAsWidget(int value)
	{
		return formatAsWidget(new Integer(value));
	}

	public String formatAsText(long value)
	{
		return formatAsText(new Long(value));
	}
	
	public Widget formatAsWidget(long value)
	{
		return formatAsWidget(new Long(value));
	}
	
	private static Date toDate(Object object)
	{
		if (object instanceof Long) {
			return new Date(((Long)object).longValue());
		}
		else if (object instanceof CalendarDate) {
			return new Date(((CalendarDate)object).getTimeInMillis());
		}
		else if (object instanceof Date) {
			return (Date)object;
		}
		else throw new IllegalArgumentException();		
	}
	
	/** Tells whether the formatted text or widget should be right-aligned.
	 */
	public boolean alignsRight()
	{
		return false;
	}

}
